package cn.daniel.goods.service.impl;

import cn.daniel.goods.entity.GoodsSort;
import cn.daniel.goods.mapper.GoodsSortMapper;
import cn.daniel.goods.service.GoodsSortService;
import cn.daniel.goods.vo.adminVo.GoodsSortAdminVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Daniel_li
 * @since 2020-12-29
 */
@Service
public class GoodsSortServiceImpl extends ServiceImpl<GoodsSortMapper, GoodsSort> implements GoodsSortService {

    @Override
    public List<GoodsSortAdminVo> getGoodsSortList() {
        QueryWrapper<GoodsSort> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id","null");
        List<GoodsSort> goodsSorts = baseMapper.selectList(wrapper);
        List<GoodsSortAdminVo> list = new ArrayList<>();
        for (GoodsSort goodsSort : goodsSorts) {
            GoodsSortAdminVo goodsSortAdminVo = new GoodsSortAdminVo();
            BeanUtils.copyProperties(goodsSort,goodsSortAdminVo);
            QueryWrapper<GoodsSort> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id",goodsSort.getSortId());
            List<GoodsSort> children = baseMapper.selectList(queryWrapper);
            goodsSortAdminVo.setChildren(children);
            list.add(goodsSortAdminVo);

        }
        return list;
    }
}
